package com.ucode.cgms.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.ucode.cgms.mapper.CgmsRebateFlowMapper;
import com.ucode.cgms.service.CgmsRebateFlowService;
import com.ucode.model.cgms.CgmsRebateFlow;
import com.ucode.model.cgms.enums.CgmsRebateStatusEnum;
import com.ucode.tool.util.IdentifyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 返点流水服务实现类
 * @Author: Claude
 * @Date: 2023/05/26 11:15
 * @Version: 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CgmsRebateFlowServiceImpl implements CgmsRebateFlowService {

    @Resource
    private CgmsRebateFlowMapper cgmsRebateFlowMapper;

    @Override
    public CgmsRebateFlowMapper initMapper() {
        return cgmsRebateFlowMapper;
    }

    @Override
    public Long createRebateFlow(Long companionId, Long orderId, Integer orderType, BigDecimal rebateAmount, 
                                 Integer rebateMode, BigDecimal rebateValue, BigDecimal orderAmount) {
        CgmsRebateFlow flow = new CgmsRebateFlow();
        flow.setId(IdentifyUtils.getDefaultSnowflakeId());
        flow.setUserId(companionId);
        flow.setOrderId(orderId);
        flow.setRebateType(orderType);
        flow.setAmount(rebateAmount);
        flow.setRebateRate(rebateValue);
        // 设置初始状态为待结算(0)
        flow.setStatuz(CgmsRebateStatusEnum.PENDING.getCode());
        // 删除标记设为未删除(false)
        flow.setDeltag(false);
        
        this.insert(flow);
        return flow.getId();
    }

    @Override
    public int updateSettleStatus(Long id, Integer settleStatus, Date settleTime) {
        return cgmsRebateFlowMapper.updateSettleStatus(id, settleStatus, settleTime);
    }

    @Override
    public int batchUpdateSettleStatus(Collection<Long> ids, Integer settleStatus, Date settleTime) {
        return cgmsRebateFlowMapper.batchUpdateSettleStatus(ids, settleStatus, settleTime);
    }

    @Override
    public int updateStatus(Collection<Long> ids, Integer status) {
        return cgmsRebateFlowMapper.updateStatus(ids, status);
    }
} 